package com.hnkywl.sts.action.htgl;

import com.hnkywl.sts.entity.htgl.Bgfkfs;
import com.hnkywl.sts.entity.htgl.Cght;
import com.hnkywl.sts.entity.htgl.Htmx;
import com.hnkywl.sts.entity.organ.Lj;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.service.htgl.CghtService;
import com.hnkywl.sts.service.organ.LjwhService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.POIUtil;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-4-8
 * Time: 下午1:43
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/htgl/bghtgl")
public class BghtglAction extends SimpleActionSupport {
    private CghtService cghtService;

    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows;
    //采购合同ID
    private Long bghtId;
    private String wzjhType;
    //当前登录员工的公司ID
    private Long gsId;

    private Cght bght;

    //查询条件
    private String zt;

    private Long hzfId;

    private Long ssbmId;

    private Long ssgsId;

    private Long jhrId;

    private String qdr;

    private Date startDate;

    private Date endDate;

    private String bghtHtbm;

    private String bghtEmwtime;

    private String htbh;

    private String wzbm;

    private List<Bgfkfs> bgfkfsList;

    private String jsrq;

    private Long htdyId;
    private Yg yg;
    private Long zggsId;
    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;
    private LjwhService ljwhService;

    @Action("bghtgl")
    public String bghtgl() {
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    /**
     * 方法名:     保供合同查询
     * 功能描述:      查询保供合同
     * 输入参数:
     * 异常:      <RuntimeException  Exception>
     * 创建人:     lingyun
     * 创建日期:   13-3-7上午11:47
     * 修改人:     lingyun
     * 修改日期:   13-3-7上午11:47
     */
    @Action("bghtList")
    public void cghtList() {
        this.renderJson(this.cghtService.findAllCgHt(wzbm, zt, "bght", htbh, ssgsId, ssbmId, jhrId, hzfId, qdr, startDate, endDate, null, page, rows));
    }

    /**
     * 方法名:     保供合同明细
     * 功能描述:      显示数据库协议名称
     * 输入参数:    <@param xyId 协议ID>
     * 异常:      <RuntimeException  Exception>
     * 创建人:     lingyun
     * 创建日期:   13-3-7上午11:47
     * 修改人:     lingyun
     * 修改日期:   13-3-7上午11:47
     */
    @Actions({@Action("bghtgl_content"), @Action("bghtgl_detail")})
    public String bghtDetail() {
        yg = YgService.getCurrentYg();
        gsId = YgService.getCurrentYgGsId();
        zggsId = YgService.getCurrentYgZgGsId();
        if (bghtId != null) {
            bght = this.cghtService.findCghtDetail(bghtId);
            htdyId = this.cghtService.findHtdyId(bghtId);
        } else {
            this.startDate = new Date();
        }
        if (null != bght && null != bght.getLj()) {
            Lj lj = this.ljwhService.find(bght.getLj().getId());
            if (null != lj && null != lj.getCghtmb()) {
                htdyId = lj.getCghtmb().getId();
                bghtHtbm = bght.getHtbm();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
                bghtEmwtime = simpleDateFormat.format(bght.getLastUpdated());
            }
        }
        return SUCCESS;
    }

    @Action("bghtgl_htmx")
    public void bghtmxContent() {
        this.renderJson(this.cghtService.findBghtmxContent(bghtId));
    }

    @Action("bghtgldetail_htmx")
    public void bghtmxDetail() {
        this.renderJson(this.cghtService.findBghtmxDetail(bghtId, page, rows));
    }

    //合同明细导出
    @Action("bghtgldetail_htmx_exp")
    public String bghtmxExp() {
        List<Map> htmx = cghtService.findCghtmx(bghtId);
        //第一步，创建一个webbook，对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("sheet1");
        POIUtil poiUtil = new POIUtil(wb);
        //设置列宽
        sheet.setDefaultRowHeightInPoints(15);
        sheet.setDefaultColumnWidth((short) 15);
        HSSFCellStyle titleStyle = poiUtil.createHeader3HSSFCellStyle(wb);
        poiUtil.fillRow(sheet, titleStyle, 0, "老物资编码", "新物资编码", "物资名称", "规格型号", "单位", "数量", "记重方式", "含税单价", "不含税单价", "含税总金额", "牌号", "交提货时间及数量");
        for (int i = 1; i < htmx.size() + 1; i++) {
            Map item = htmx.get(i - 1);
            poiUtil.fillRow(sheet, null, i, item.get("wzgg.oldcode"), item.get("wzgg.code"), item.get("wzgg.name"), item.get("wzgg.ggxh"),
                    item.get("wzgg.dw"), item.get("sbsl"), item.get("jzfs.z"), item.get("hsj"), item.get("bhsj"), item.get("hsje"), item.get("ph"), item.get("jthsjsl"));
        }
        try {
            response.setContentType("application/x-download");
            String filedisplay = new String(("保供合同明细.xls").getBytes("gb2312"), "iso8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            wb.write(response.getOutputStream());
            response.getOutputStream().close();
        } catch (Exception e) {
            e.printStackTrace();
            renderText("位置异常，导出失败" + e.getMessage());
        }
        return null;
    }


    @Action("bght_fkfsList")
    public void bghtFkfsList() {
        if (bghtId != null) {
            renderJson(cghtService.findBgFkfsList(bghtId));
        } else {
            renderJson(cghtService.findBgFkfsList(Long.parseLong("0")));
        }
    }

    @Action("add_bght")
    public void addBght() {
        try {
            initCght(bght);
            this.cghtService.saveBght(bght);
            renderText("{\"statusCode\":\"200\", \"message\":\"添加保供合同成功！\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("edit_bght")
    public void editCght() {
        try {
            Cght clone = this.cghtService.findCghtDetail(bght.getId());
            String htbm = clone.getHtbm();
            clone.setZjhtbz(bght.getZjhtbz());
            clone.setCgjs(bght.getCgjs());
            clone.setQddd(bght.getQddd());
            clone.setJsrq(bght.getJsrq());
            clone.setFkfs(bght.getFkfs());
            clone.setJhsj(bght.getJhsj());
            clone.setJhdd(bght.getJhdd());
            clone.setJhfs(bght.getJhfs());
            clone.setFjList(bght.getFjList());
            clone.setBgFkfs(bght.getBgFkfs());
            initCght(clone);
            clone.setZt("ybc");
            this.cghtService.editBght(clone, htbm);
            renderText("{\"statusCode\":\"200\", \"message\":\"修改保供合同成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("bghtgl_yq")
    public String bgjhywk_yq() {
        bght = this.cghtService.findCghtDetail(bghtId);
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
        if (bght.getJsrq() != null)
            jsrq = simple.format(bght.getJsrq());
        return SUCCESS;
    }

    //合同延期
    @Action("htyq_bght")
    public void ywk_jhyq() {
        try {
            this.cghtService.saveHtyq(bght);
            renderText("{\"statusCode\":\"200\",\"message\":\"合同延期成功！\"}");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }


    private void initCght(Cght cght) {
        if (cght.getWzjh() != null && cght.getWzjh().getId() == null) {
            cght.setWzjh(null);
        }
        if (cght.getLj() != null && cght.getLj().getId() == null) {
            cght.setLj(null);
        }
        if (cght.getHzf() != null && cght.getHzf().getId() == null) {
            cght.setHzf(null);
        }
        if (cght.getFkfs() != null && cght.getFkfs().getId() == null) {
            cght.setFkfs(null);
        }
        if (cght.getHtxz() != null && cght.getHtxz().getId() == null) {
            cght.setHtxz(null);
        }
//        if (cght.getZddhxsht() != null && cght.getZddhxsht().getId() == null) {
//            cght.setZddhxsht(null);
//        }
        if (cght.getSbgs() != null && cght.getSbgs().getId() == null) {
            cght.setSbgs(null);
        }
        if (cght.getSbbm() != null && cght.getSbbm().getId() == null) {
            cght.setSbbm(null);
        }
        if (cght.getSbr() != null && cght.getSbr().getId() == null) {
            cght.setSbr(null);
        }
        if (cght.getWlfgs() != null && cght.getWlfgs().getId() == null) {
            cght.setWlfgs(null);
        }
        if (cght.getWlfbm() != null && cght.getWlfbm().getId() == null) {
            cght.setWlfbm(null);
        }
        if (cght.getWlfry() != null && cght.getWlfry().getId() == null) {
            cght.setWlfry(null);
        }
        if (cght.getFkfs() != null && cght.getFkfs().getId() == null) {
            cght.setFkfs(null);
        }
        if (cght.getZjhtbz() != null && cght.getZjhtbz().getId() == null) {
            cght.setZjhtbz(null);
        }
        if (cght.getCgjs() != null && cght.getCgjs().getId() == null) {
            cght.setCgjs(null);
        }
        if (cght.getHtmxList() != null) {
            for (Htmx htmx : cght.getHtmxList()) {
                if (htmx.getCd() != null && htmx.getCd().getId() == null) {
                    htmx.setCd(null);
                }
                if (htmx.getJzfs() != null && htmx.getJzfs().getId() == null) {
                    htmx.setJzfs(null);
                }
            }
        }
    }

//    @Action("bght_savefkfs")
//    public void saveFkfs() {
//        try {
//            cghtService.saveBgfkfs(bgfkfsList, bghtId);
//            renderText("{\"statusCode\":200}");
//        } catch (Exception e) {
//            e.printStackTrace();
//            renderText("{\"statusCode\":300,\"message\":\"" + e.getMessage() + "\"}");
//        }
//    }

    @Action("delete_bght")
    public void delete_bght() {
        try {
            this.cghtService.deleteCght(bghtId);
            renderText("{\"statusCode\":\"200\", \"message\":\"删除保供合同成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 审核保供合同
     */
    @Action("checkBght")
    public void checkBght() {
        try {
            cghtService.checkBght(bghtId);
            renderText("{\"statusCode\":\"200\", \"message\":\"审核保供合同成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":300,\"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 方法名:     履约保供合同
     * 功能描述:      履约数据库采购合同
     * 输入参数:    <@param cghtId 采购合同ID>
     * 异常:      <RuntimeException  Exception>
     * 创建人:     lingyun
     * 创建日期:   13-3-7上午11:47
     * 修改人:     lingyun
     * 修改日期:   13-3-7上午11:47
     */
    @Action("agree_bght")
    public void agree_bght() {
        try {
            this.cghtService.agreeCght(bghtId);
            renderText("{\"statusCode\":\"200\", \"message\":\"履约采购合同成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    /**
     * 方法名:     终止保供合同
     * 功能描述:      终止数据库采购合同
     * 输入参数:    <@param cghtId 采购合同ID>
     * 异常:      <RuntimeException  Exception>
     * 创建人:     lingyun
     * 创建日期:   13-3-7上午11:47
     * 修改人:     lingyun
     * 修改日期:   13-3-7上午11:47
     */
    @Action("stop_cght")
    public void stop_cght() {
        try {
            this.cghtService.stopCght(bghtId);
            renderText("{\"statusCode\":\"200\", \"message\":\"终止采购合同成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Long getBghtId() {
        return bghtId;
    }

    public void setBghtId(Long bghtId) {
        this.bghtId = bghtId;
    }

    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }

    public Cght getBght() {
        return bght;
    }

    public void setBght(Cght bght) {
        this.bght = bght;
    }

    public String getZt() {
        return zt;
    }

    public void setZt(String zt) {
        this.zt = zt;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public Long getHzfId() {
        return hzfId;
    }

    public void setHzfId(Long hzfId) {
        this.hzfId = hzfId;
    }

    public Long getSsbmId() {
        return ssbmId;
    }

    public void setSsbmId(Long ssbmId) {
        this.ssbmId = ssbmId;
    }

    public Long getSsgsId() {
        return ssgsId;
    }

    public void setSsgsId(Long ssgsId) {
        this.ssgsId = ssgsId;
    }

    public String getQdr() {
        return qdr;
    }

    public void setQdr(String qdr) {
        this.qdr = qdr;
    }

    public Yg getYg() {
        return yg;
    }

    public void setYg(Yg yg) {
        this.yg = yg;
    }

    public Long getJhrId() {
        return jhrId;
    }

    public void setJhrId(Long jhrId) {
        this.jhrId = jhrId;
    }

    public String getHtbh() {
        return htbh;
    }

    public void setHtbh(String htbh) {
        this.htbh = htbh;
    }

    public List<Bgfkfs> getBgfkfsList() {
        return bgfkfsList;
    }

    public void setBgfkfsList(List<Bgfkfs> bgfkfsList) {
        this.bgfkfsList = bgfkfsList;
    }

    public String getJsrq() {
        return jsrq;
    }

    public void setJsrq(String jsrq) {
        this.jsrq = jsrq;
    }

    public Long getHtdyId() {
        return htdyId;
    }

    public void setHtdyId(Long htdyId) {
        this.htdyId = htdyId;
    }

    public String getWzjhType() {
        return wzjhType;
    }

    public void setWzjhType(String wzjhType) {
        this.wzjhType = wzjhType;
    }

    public Long getZggsId() {
        return zggsId;
    }

    public String getBghtHtbm() {
        return bghtHtbm;
    }

    public void setBghtHtbm(String bghtHtbm) {
        this.bghtHtbm = bghtHtbm;
    }

    public String getBghtEmwtime() {
        return bghtEmwtime;
    }

    public void setBghtEmwtime(String bghtEmwtime) {
        this.bghtEmwtime = bghtEmwtime;
    }

    public String getWzbm() {
        return wzbm;
    }

    public void setWzbm(String wzbm) {
        this.wzbm = wzbm;
    }

    public void setZggsId(Long zggsId) {
        this.zggsId = zggsId;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    @Autowired
    public void setCghtService(CghtService cghtService) {
        this.cghtService = cghtService;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

    @Autowired
    public void setLjwhService(LjwhService ljwhService) {
        this.ljwhService = ljwhService;
    }
}
